package com.nashuju.web.service.role;

import java.util.Collections;
import java.util.List;

import org.durcframework.core.expression.ExpressionQuery;
import org.durcframework.core.expression.subexpression.ValueExpression;
import org.durcframework.core.service.CrudService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.nashuju.web.dao.role.UserRoleDao;
import com.nashuju.web.entity.role.UserRole;
import com.nashuju.web.entity.role.UserRoleParam;

@Service
public class UserRoleService extends CrudService<UserRole, UserRoleDao> {

    /**
     * 设置用户角色
     */
    public void setUserRole(UserRoleParam userRoleParam) {

        if (StringUtils.isEmpty(userRoleParam.getUsername()) || CollectionUtils.isEmpty(userRoleParam.getRoleIds())) {
            return;
        }

        this.getDao().delAllUserRole(userRoleParam.getUsername());

        this.getDao().setUserRole(userRoleParam);
    }

    /**
     * 获取用户角色
     * @param username
     * @return
     */
    public List<UserRole> getUserRole(String username) {
        if (StringUtils.isEmpty(username)) {
            return Collections.emptyList();
        }
        ExpressionQuery query = ExpressionQuery.buildQueryAll();
        query.add(new ValueExpression("username", username));

        return this.find(query);
    }

    public List<UserRole> getUserRoleByRoleId(int roleId) {
        ExpressionQuery query = ExpressionQuery.buildQueryAll();
        query.add(new ValueExpression("role_id", roleId));
        return this.find(query);
    }

    public void delByRoleId(int roleId) {
        this.getDao().delByRoleId(roleId);
    }

}
